﻿using Realtors.Repository;
using Realtors.Web.UI.Infrastructure;
using Realtors.Web.UI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Realtors.Web.UI.Controllers
{
    public class ContactController : Controller
    {
        private IMailService MailService { get; set; }
        private IHouseRepository houseRepository { get; set; }

        public ContactController(IHouseRepository db,IMailService mailService)
        {
            this.MailService = mailService;
            houseRepository = db;
        }

        [HttpGet]
        public ActionResult Contact()
        {
            ContactUsViewModel model = new ContactUsViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Contact(ContactUsViewModel model)
        {            
            string subject = string.Format("Message from :{0}", model.Name);

            if (MailService.Send(model.EmailAddress, subject, model.Message))
            {
                ViewBag.SentMessage = true;
            }
            return View(model);
        }

        [HttpGet]
        public ViewResult SendMessageeForHouse(Guid id)
        {
            var house = houseRepository.GetHouseByID(id);
            
            if (house.Active == false)
                throw new ArgumentException("This listing is not active anymore!");


            ContactForUnitViewModel model = new ContactForUnitViewModel();
            model.UnitID = house.EntityId;
            model.UnitTitle = house.Title;
          
            return View(model);
        }

        [HttpPost]
        public ActionResult SendMessageForHouse(ContactForUnitViewModel model)
        {
            string subject = string.Format("Message from :{0}", model.Name);
            string body = String.Format("Message for unit: {0} {1}    {2}  {3}",model.UnitTitle,
                Url.Action("Detail","Home",new{id=model.UnitID},Request.Url.Scheme),Environment.NewLine,model.Message);
            if (MailService.Send(model.EmailAddress, subject, model.Message))
            {
                ViewBag.SentMessage = true;
            }
            return View(model);
        }
	}
}